package com.feixiu.service.impl;

import com.feixiu.dao.ApprovalRecordsDao;
import com.feixiu.dao.ProcessControlDao;
import com.feixiu.dao.StaffDao;
import com.feixiu.exception.ApprovalException;
import com.feixiu.pojo.ApprovalRecords;
import com.feixiu.pojo.ProcessControl;
import com.feixiu.service.ApprovalRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Iterator;
import java.util.List;
@Service
public class ApprovalRecordsServiceImpl implements ApprovalRecordsService {
    @Autowired
    private ProcessControlDao processControlDao;
    @Autowired
    private ApprovalRecordsDao approvalRecordsDao;
    @Autowired
    private StaffDao staffDao;
    @Override
    public List<ApprovalRecords> getAllRecords(String staffno) {
        List<ApprovalRecords> list=approvalRecordsDao.selectAllRecordsByStaff(staffno);
        list.forEach(record->{
            if (record.getFinished().equals("0")){
               String approvalno= approvalRecordsDao.selectUnApprovalno(record.getRecordno().toString()).getApprovalno();
               String name=staffDao.selectById(approvalno).getStaffname();
               record.setRecordopinion1(name+"尚未审批");
               record.setFinished("未审批");
            }else if (record.getFinished().equals("1")){

                record.setFinished("审批通过");
            }else if (record.getFinished().equals("2")){
                record.setFinished("驳回");
            }
            String name1=staffDao.selectById(record.getStaffno()).getStaffname();
            String name2=staffDao.selectById(record.getApprovalno()).getStaffname();
            record.setStaffno(name1);
            record.setApprovalno(name2);

        });
        return list;
    }

    @Override
    public List<ApprovalRecords> getAllApprovalRecords(String staffno) {
        List<ApprovalRecords> list=approvalRecordsDao.selectAllRecordsByApproval(staffno);
        int i=0;
        while (i<list.size()){
            if (list.get(i).getFinished().equals("0")){
                /**
                 * 如果  finished==0；那么证明当前审批人没有审批这个申请，但是如果上一个人也没有审批，那就轮不到我审批
                 * 所以   approvalRecordsDao.selectUnApprovalno   获取第一个没有审批这个申请的人
                 * 如果那个人是我，那就轮到我审批了，如果不是，那就移除这个记录，因为没有轮到我审批
                 */
                String unApprovalno=approvalRecordsDao.selectUnApprovalno(list.get(i).getRecordno().toString()).getApprovalno();
                if (unApprovalno.equals(list.get(i).getApprovalno())){
                    list.get(i).setRecordopinion1("等待你审批");
                    list.get(i).setFinished("未审批");
                }else{
                    list.remove(i);

                    continue;

                }

            }else if (list.get(i).getFinished().equals("1")){

                list.get(i).setFinished("审批通过");
            }else if (list.get(i).getFinished().equals("2")){
                list.get(i).setFinished("驳回");
            }
            String name1=staffDao.selectById(list.get(i).getStaffno()).getStaffname();
            list.get(i).setStaffno(name1);
            i++;
        }

        return list;
    }

    /**
     * 根据recordno  调用  selectFinishApproval获取所有已审批的
     * 但是如果当前记录就是第一个没有审批的，就会返回空，所以需要判断
     * 如果返回不是空就正常操作，如果是空，就把当前记录的详细信息返回
     * @param recordno
     * @return
     */
    @Override
    public List<ApprovalRecords> getFinishRecords(String recordno) {
        List<ApprovalRecords> list = approvalRecordsDao.selectFinishApproval(recordno);
        if (list.size()!=0) {
            list.forEach(record -> {

                String name1 = staffDao.selectById(record.getStaffno()).getStaffname();
                String name2 = staffDao.selectById(record.getApprovalno()).getStaffname();

                record.setStaffno(name1);
                record.setApprovalno(name2);
            });
        } else {
            list.add(approvalRecordsDao.selectUnApprovalno(recordno));
            list.forEach(record -> {

                String name1 = staffDao.selectById(record.getStaffno()).getStaffname();
                String name2 = staffDao.selectById(record.getApprovalno()).getStaffname();

                record.setStaffno(name1);
                record.setApprovalno(name2);
            });

        }
        return list;
    }

    @Override
    public Boolean refuse(ApprovalRecords approvalRecords) {
        boolean flag=false;
        //ApprovalRecords app=approvalRecordsDao.selectUnApprovalno(approvalRecords.getRecordno().toString());
         String newOp= staffDao.selectById(approvalRecords.getApprovalno()).getStaffname()+"拒绝的！原因是:"+
                 approvalRecords.getRecordopinion1();
         approvalRecords.setRecordopinion1(newOp);
        int count=approvalRecordsDao.updateFail(approvalRecords);
        if (count>0){
            flag=true;
        }
        return flag;
    }

    @Override
    public Boolean agree(ApprovalRecords approvalRecords) {
        Boolean flag=false;
        int count=approvalRecordsDao.updateAgree(approvalRecords);
        if (count>0){
            flag=true;
        }

        return flag;
    }

    /**
     * 这个service操作主要涉及到了两个表，多行记录
     * 首先要到processcontrol表中，根据processname对申请流程进行查询，获取申请流程
     * 然后根据申请流程到approvalrecord表中创建申请记录
     * 涉及到了多个表，多行记录，就需要用到事务了。保证数据库操作的正常执行
     * 事务的话，有两种，一种就是spring自己带着的事务注解方式
     * 一种就是aop，异常的话，就是mybatis的异常   IbatisException.class
     * @param processname
     * @param staffno
     * @param opinion
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ,rollbackFor = ApprovalException.class)
    public Boolean approvalAdd(String processname, String staffno, String opinion) {
        if (processname==null||staffno==null||opinion==null){
            new ApprovalException("信息没有给完整");
        }
        Boolean flag=false;
        List<ProcessControl> list=processControlDao.selectByName(processname);
        if (list==null){
            new ApprovalException("没有找到相关的申请流程！！！");
        }
        String  mainrecord="";
        String s=""+((int)Math.random()*1000000);
        LocalDateTime localDateTime=LocalDateTime.now();
        int i=0;
        for (i=0;i<list.size();i++){

            ApprovalRecords approvalRecords=new ApprovalRecords();
            approvalRecords.setStaffno(staffno);
            approvalRecords.setRecordname(processname);
            approvalRecords.setApprovalno(list.get(i).getApprovalno().toString());
            approvalRecords.setCurrentrecord(list.get(i).getCurrentstep());
            approvalRecords.setFinished("0");
            approvalRecords.setTime(localDateTime.toString());
            approvalRecords.setRecordopinion(opinion);
            approvalRecords.setMainrecord(s);
            int count=approvalRecordsDao.insert(approvalRecords);
            if (count==0){
                new ApprovalException("插入新纪录失败失败");
            }
            if (i==0){
                mainrecord=approvalRecords.getRecordno().toString();
            }
        }
       int c= approvalRecordsDao.setMainRecord(mainrecord,s);
        if (c==0){
            new ApprovalException("更新主记录号失败");
        }
        flag=true;
        return flag;
    }
}
